<template>
  <div>
    <el-card class="mb">
      <div class="title mb">大屏配置</div>
      <el-form :inline="true" label-width="100px" :model="form">
        <el-form-item label="大屏名称：">
          <el-input
            clearable
            placeholder="请输入"
            v-model="form.reportName"
          ></el-input>
        </el-form-item>
        <el-form-item label="创建时间：">
          <el-date-picker
            v-model="form.createTime"
            type="datetime"
            placeholder="请选择"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button
        icon="el-icon-plus"
        type="primary"
        class="mb"
        @click="dlalogOpen({}, 'add')"
        v-if="!onlyLook"
        >新建</el-button
      >
      <LargeScreenTable
        :tableData="tableData"
        :tabtitle="tabtitle"
        :loading="loading"
        height="calc(100vh - 330px)"
        :current="pagination.pageNum"
        :pageSize="pagination.pageSize"
        :total="pagination.total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
        <el-table-column
          align="center"
          label="操作"
          width="200"
          v-if="!onlyLook"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="dlalogOpen(scope.row, 'relate')"
              >关联指标</el-button
            >
            <el-button type="text" @click="dlalogOpen(scope.row, 'edit')"
              >编辑</el-button
            >

            <el-button
              type="text"
              :disabled="scope.row.type != '0'"
              @click="del(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </LargeScreenTable>
    </el-card>
    <AddEdit ref="addEdit"></AddEdit>
  </div>
</template>

<script>
import LargeScreenTable from '@/components/Table.vue'
import AddEdit from './components/AddEdit.vue'
import { findAll, deleteById } from '@/apis/largeScreenConfig.js'
import { getDetailByAccountOrId, getUserInfo, getSelf } from '@/apis/Common.js'
import { getRealToken } from '@/utils/util'

export default {
  components: {
    LargeScreenTable,
    AddEdit
  },
  data() {
    return {
      form: {
        reportName: '',
        createTime: ''
      },
      codeList: [],
      tableData: [],
      loading: false,
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      onlyLook: false,
      tabtitle: [
        { prop: 'reportCode', label: '大屏编码' },
        { prop: 'reportName', label: '大屏名称' },
        { prop: 'url', label: '链接地址' },
        { prop: 'param', label: '参数' },
        {
          prop: 'createTime',
          label: '创建时间',
          sortable: true,
          width: 180
        },
        {
          prop: 'type',
          label: '状态',
          width: 100,
          render: (h, scope) => {
            return h(
              'div',
              {
                attrs: {
                  class: scope.row.type
                    ? scope.row.type === '0'
                      ? 'type-normal'
                      : 'type-warning'
                    : ''
                }
              },
              [scope.row.typeDes]
            )
          }
        }
      ]
    }
  },
  mounted() {
    this.getTableData()
    this.getUserRole()
  },
  methods: {
    async getUserRole() {
      try {
        const accessObj = getRealToken()
        const isCheckOut = accessObj?.isCheckOut
        let res = null
        if (isCheckOut) {
          const resInfo = await getSelf()
          res = await getUserInfo(resInfo.tenantId)
        } else {
          res = await getDetailByAccountOrId({
            account: sessionStorage.getItem('currentUser')
              ? JSON.parse(sessionStorage.getItem('currentUser')).account
              : null
          })
        }
        if (res?.role?.roleCode?.length > 0) {
          const roleCode = res.role.roleCode.split('|')
          const arr = roleCode
            .map((roleCode) =>
              ['hygczmkcy-gcy', 'hygczmkjt-gcy'].indexOf(roleCode)
            )
            .filter((index) => index !== -1)
          if (arr.length > 0) {
            this.onlyLook = true
          } else {
            this.onlyLook = false
          }
        } else {
          this.onlyLook = true
        }
      } catch (error) {
        this.onlyLook = true
      }
    },
    getTableData() {
      this.loading = true
      const { createTime, reportName } = this.form
      const { pageNum, pageSize } = this.pagination
      findAll({ createTime, reportName, pageNum, pageSize })
        .then((res) => {
          if (res.code === 200) {
            this.tableData = res.data.list
            this.pagination.total = res.data.total
          }
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    search() {
      this.pagination.pageNum = 1
      this.getTableData()
    },
    reset() {
      this.form = this.$options.data().form
      this.search()
    },
    del(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteById({ id: row.id }).then((res) => {
            console.log(res)
            if (res.code === 200) {
              this.getTableData()
              this.$message.success('删除成功')
            } else {
              this.$message.warning(res.data || '删除失败!')
            }
          })
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    dlalogOpen(row, type) {
      this.$refs.addEdit.show(row, type)
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.pagination.pageNum = val
      this.getTableData()
    }
  }
}
</script>

<style lang="less" scoped>
.title {
  font-size: 20px;
  font-weight: bold;
}
.mb {
  margin-bottom: 15px;
}
</style>
